<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Document</title>
  <style>
    .swiper {
      width: 500px;
      height: 300px;
      border: 1px solid #000;
      overflow: hidden;
      position: relative;
    }

    .swiperContent {
      display: flex;
      height: 100%;
      transition: all 1s ease;
      transform: translateX(-500px);
    }

    .item {
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100%;
      font-size: 50px;
    }

    .item1 {
      background-color: red;
    }

    .item2 {
      background-color: skyblue;
    }

    .item3 {
      background-color: green;
    }

    .item4 {
      background-color: blueviolet;
    }

    button {
      position: absolute;
      width: 25px;
      height: 60px;
      font-size: 20px;
      border: none;
      background-color: rgba(3, 3, 3, 0.5);
      color: #fff;
      border-radius: 10px;
    }

    .leftBtn {
      left: 10px;
      top: 50%;
      transform: translateY(-50%);
    }

    .rightBtn {
      right: 10px;
      top: 50%;
      transform: translateY(-50%);
    }

    .indicator {
      background-color: hsla(0, 0%, 100%, 0.4);
      border-radius: 20px;
      bottom: 10px;
      display: flex;
      left: 50%;
      padding: 5px;
      position: absolute;
      transform: translate(-50%);
      z-index: 100;
      list-style: none;
    }

    .indicator li {
      width: 50px;
      height: 50px;
      border-radius: 10px;
      background-color: rgba(255, 255, 255, 0.7);
      margin: 5px;
      transition: all 0.5s ease;
    }

    .indicator .active {
      background-color: #036cf8;
      width: 100px;
    }
  </style>
</head>

<body>
  <div class="swiper">
    <div class="swiperContent">
      <div class="item item1">1</div>
      <div class="item item2">2</div>
      <div class="item item3">3</div>
      <div class="item item4">4</div>
    </div>
    <button class="leftBtn" data-btn="left">&lt;</button>
    <button class="rightBtn" data-btn="right">&gt;</button>
    <ul class="indicator"></ul>
  </div>

  <script>
    // 获取元素
    let swiper = document.querySelector(".swiper");
    let swpCot = document.querySelector(".swiperContent");
    let items = document.querySelectorAll(".item");
    let Lbtn = document.querySelector(".leftBtn");
    let Rbtn = document.querySelector(".rightBtn");
    let ul = document.querySelector(".indicator");

    //  克隆节点
    let clone1 = items[0].cloneNode(1);
    let clone2 = items[items.length - 1].cloneNode(1);
    swpCot.appendChild(clone1);
    swpCot.insertBefore(clone2, items[0]);
    items = document.querySelectorAll(".item");

    // 设置宽度
    let w =
      swpCot.children.length *
      window.getComputedStyle(swiper).width.slice(0, -2);
    console.log(w);
    swpCot.style.width = w + "px";
    let itemW = w / items.length;
    items.forEach(function (item) {
      item.style.width = itemW + "px";
    });

    let count = 1;
    let isClick = true;

    swpCot.ontransitionend = function () {
      isClick = true;
      if (count > items.length - 2) {
        count = 1;
        swpCot.style.transitionDuration = "0s";
        swpCot.style.transform = `translateX(-${count * itemW}px)`;
      } else if (count < 1) {
        count = 4;
        swpCot.style.transitionDuration = "0s";
        swpCot.style.transform = `translateX(-${count * itemW}px)`;
      }
    };

    Lbtn.onclick = function () {
      if (!isClick) return;
      isClick = false;
      count--;
      swpCot.style.transitionDuration = "1s";
      swpCot.style.transform = `translateX(-${count * itemW}px)`;
      if (count > 4) {
        checkIdx(0);
      } else if (count < 1) {
        checkIdx(items.length - 3);
      } else {
        checkIdx(count - 1);
      }
    };
    Rbtn.onclick = function () {
      if (!isClick) return;
      isClick = false;
      count++;
      swpCot.style.transitionDuration = "1s";
      swpCot.style.transform = `translateX(-${count * itemW}px)`;
      if (count > 4) {
        checkIdx(0);
      } else if (count < 1) {
        checkIdx(items.length - 3);
      } else {
        checkIdx(count - 1);
      }
    };

    // 创建指示器
    items.forEach(function (item, index) {
      if (index < items.length - 2) {
        let li = document.createElement("li");
        if (index == count - 1) li.classList.add("active");
        ul.appendChild(li);
      }
    });

    // 切换指示器激活态函数
    function checkIdx(idx) {
      for (let i = 0; i < ul.children.length; i++) {
        ul.children[i].className = "";
      }
      ul.children[idx].classList.add("active");
    }

    // 点击指示器切换轮播图
    for (let i = 0; i < ul.children.length; i++) {
      ul.children[i].onclick = function (e) {
        if (!isClick) return;
        isClick = false;
        count = i + 1;
        swpCot.style.transitionDuration = "1s";
        swpCot.style.transform = `translateX(-${count * itemW}px)`;
        checkIdx(count - 1)
      };
    }
  </script>
</body>

</html>